//
//  SISSReceivedCommentViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/5/3.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSReceivedCommentViewController.h"
#import "SISSOrderEvaluateViewCell.h"
#import "SISSNetworkingManager+Comment.h"

static NSString *receivedCommentCellReuseID = @"receivedCommentCellReuseID";
@interface SISSReceivedCommentViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet SISSBaseTableView *tableView;

/* 评分模型 */
@property (strong, nonatomic) SISSCommentScoreModel *commentScoreModel;
/* 评论数组 */
@property (strong, nonatomic) NSMutableArray<SISSCommentModel *> *commentArr;

@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet UILabel *CommentCountLabel;

@property (assign, nonatomic) NSInteger counts;

/** pageNo */
@property (assign, nonatomic) NSInteger pageNo;
/// commentType 1带看 2服务
@property (assign, nonatomic) NSInteger commentType;

@end

@implementation SISSReceivedCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageNo = 1;
    self.commentType = 1;
    [self requestScoreAndCommentCountDataWithCommentType:1];
    [self requestTakeLookCommentData];
    
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf upDownLoadData];
    }];
    
    
    [kSISSRefresh prepareFooterRefresh:self.tableView reloadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeReloadMore;
        [weakSelf reloadMoreData];
    }];
}

- (void)setSpId:(NSString *)spId {
    _spId = spId;
    self.pageNo = 1;
    [self requestScoreAndCommentCountDataWithCommentType:1];
    [self requestTakeLookCommentData];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    [tableView tableViewDisplayWithMessage:@"没有相关数据！" ifNecessaryForRowCount:self.commentArr.count];
    return self.commentArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSOrderEvaluateViewCell *cell = [tableView dequeueReusableCellWithIdentifier:receivedCommentCellReuseID forIndexPath:indexPath];
    cell.commentModel = self.commentArr[indexPath.row];
        
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    // tableView相关
    // 自动行高
    self.tableView.estimatedRowHeight = 140;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }
    // 去掉cell系统的线条
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSOrderEvaluateViewCell" bundle:nil] forCellReuseIdentifier:receivedCommentCellReuseID];
}

#pragma mark - 上下拉刷新加载方法
- (void)upDownLoadData {
    self.pageNo = 1;
    if (self.commentType == 1) {
        [self requestTakeLookCommentData];
    } else if (self.commentType == 2) {
        [self requsetServiceCommentData];
    }
}

- (void)reloadMoreData {
    
    self.pageNo += 1;
    if (self.commentType == 1) {
        [self requestTakeLookCommentData];
    } else if (self.commentType == 2) {
        [self requsetServiceCommentData];
    }
}

#pragma mark - 请求数据
/**
 请求评分和评论人数

 @param commentType 1:带看评价  2:服务评价
 */
- (void)requestScoreAndCommentCountDataWithCommentType:(NSInteger)commentType {
    
    NSString *spId = self.spId ? self.spId : [SISSUserAccount shared].spId;
    
    [kNetworkManager requestAgentReceivedCommentInfoWithCommentType:commentType spId:spId success:^(SISSCommentScoreModel *commentScoreModel) {
        
        self.commentScoreModel = commentScoreModel;
        
        self.scoreLabel.text = commentScoreModel.score.length > 0 ? commentScoreModel.score : @"0";
        self.CommentCountLabel.text = [NSString stringWithFormat:@"%zd",commentScoreModel.count];
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}



/**
 请求带看评论数据
 */
- (void)requestTakeLookCommentData  {
    
    NSString *spId = self.spId ? self.spId : [SISSUserAccount shared].spId;
    
    WeakSelf
    [kNetworkManager requestAgentReceivedTakeLookCommentInfoWithPageNo:self.pageNo spId:spId success:^(SISSCommentBaseModel *commentBaseModel) {
     
        if (weakSelf.commentArr.count != 0 && kSISSRefresh.refreshType == SISSRefressTypeUpDown) {
            
            [weakSelf.commentArr removeAllObjects];
        }
        
        for (int i = 0; i < commentBaseModel.data.count; i++) {
            
            [weakSelf.commentArr addObject:commentBaseModel.data[i]];
        }
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf.tableView reloadData];
            
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshing];
            if (weakSelf.commentArr.count >= commentBaseModel.total) {
//                weakSelf.pageNo = commentBaseModel.totalPages;
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            if (weakSelf.commentArr.count == 0) {
                self.tableView.mj_footer.hidden = YES;
            }
        }];
     
 } failed:^(NSError *error) {
     [weakSelf.tableView.mj_header endRefreshing];
     [weakSelf.tableView.mj_footer endRefreshing];
     NSLog(@"%@",error);
 }];
}

/**
 请求服务评价数据
 */
- (void)requsetServiceCommentData {
    
    NSString *spId = self.spId ? self.spId : [SISSUserAccount shared].spId;
    WeakSelf
    [kNetworkManager requestAgentReceivedServiceCommentInfoWithPageNo:self.pageNo spId:spId success:^(SISSCommentBaseModel *commentBaseModel) {
        
        if (weakSelf.commentArr.count != 0 && kSISSRefresh.refreshType == SISSRefressTypeUpDown) {
            
            [weakSelf.commentArr removeAllObjects];
        }
        
        for (int i = 0; i < commentBaseModel.data.count; i++) {
            
            [weakSelf.commentArr addObject:commentBaseModel.data[i]];
        }
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf.tableView reloadData];
            
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshing];
            if (weakSelf.commentArr.count >= commentBaseModel.total) {
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            if (weakSelf.commentArr.count == 0) {
                self.tableView.mj_footer.hidden = YES;
            }
        }];
        
    } failed:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        NSLog(@"%@",error);
    }];
    
}

#pragma mark - 点击segmentControl的响应事件
- (IBAction)segmentControlClickAction:(UISegmentedControl *)sender {
    self.pageNo = 1;
    if (sender.selectedSegmentIndex == 0) {
        self.commentType = 1;
        [self requestScoreAndCommentCountDataWithCommentType:1];
        [self requestTakeLookCommentData];
    } else {
        self.commentType = 2;
        [self requestScoreAndCommentCountDataWithCommentType:2];
        [self requsetServiceCommentData];
    }
    
    [self.tableView reloadData];
    NSLog(@"selectedSegmentIndex:  %zd",sender.selectedSegmentIndex);
}

- (NSMutableArray<SISSCommentModel *> *)commentArr {
    if (!_commentArr) {
        _commentArr = [NSMutableArray array];
    }
    return _commentArr;
}

@end
